package br.com.edumobi.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import br.com.edumobi.P;

public abstract class Dates {

    public static final String FORMATO_DD_MM_YYYY = "dd/MM/yyyy";
    public static final String FORMATO_YYYY_MM_DD = "yyyy-MM-dd";
    public static final String FORMATO_HH_MM_SS = "hh:mm:ss";
    public static final String FORMATO_MM_SS = "mm:ss";

    private static SimpleDateFormat formatoData = new SimpleDateFormat(FORMATO_DD_MM_YYYY, P.LOCALE_DEFAULT);

    public static Date agora() {
        return new Date();
    }

    public static String dateParaString(Date date) {
        if (date != null) {
            return formatoData.format(date);
        } else {
            return null;
        }
    }

    public static String dateParaString(Date date, String formato) {
        if (date != null) {
            if (formato != null && !formato.trim().equals("")) {
                SimpleDateFormat dateFormat = new SimpleDateFormat(formato, P.LOCALE_DEFAULT);
                return dateFormat.format(date);
            }
        }
        return null;
    }

    public static Date stringParaDate(String string) {
        if (string != null && !string.isEmpty()) {
            try {
                return formatoData.parse(string);
            } catch (ParseException excecao) {
                return null;
            }
        } else {
            return null;
        }
    }

    public static Date stringParaDate(String string, String formato) {
        if (string != null && !string.isEmpty()) {
            if (formato != null && !formato.isEmpty()) {
                SimpleDateFormat formatoData = new SimpleDateFormat(formato, P.LOCALE_DEFAULT);
                try {
                    return formatoData.parse(string);
                } catch (ParseException excecao) {
                    return null;
                }
            }
        }
        return null;
    }
}